﻿#Region "Copyright"

' Copyright 2007 Ben Reichelt - http://benreichelt.net
' 
' Licensed under the Apache License, Version 2.0 (the "License");
' you may not use this file except in compliance with the License.
' You may obtain a copy of the License at
' 
'     http://www.apache.org/licenses/LICENSE-2.0
' 
' Unless required by applicable law or agreed to in writing, software
' distributed under the License is distributed on an "AS IS" BASIS,
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' See the License for the specific language governing permissions and
' limitations under the License.


#End Region

Imports NUnit.Framework
Imports SqlPlastic
Imports System.Xml.Linq

<TestFixture()> _
Public Class TaskTests

    Private metaData As XmlMetaData
    Private context As AppContext

    <SetUp()> _
    Public Sub Setup()
        metaData = ObjectMother.CreateDefaultMetaData
        context = New AppContext
        context.Logger = New ConsoleLogger
    End Sub

    <Test()> _
    Public Sub RenameTableTest()
        Dim task As New RenameTableTask(C.Table1Name, "Ben", 1)
        task.Execute(metaData, context)
        Dim table As MetaDataTable = metaData.FindTable(C.Table1Name)
        Assert.AreEqual("Ben", table.MemberName)
    End Sub

    <Test()> _
    Public Sub RenameTypeTest()
        Dim task As New RenameTypeTask(C.Type1Name, "Ben", 1)
        task.Execute(metaData, context)
        Dim type As MetaDataType = metaData.FindType("Ben")
        Assert.IsNotNull(Type)
    End Sub

    <Test()> _
    Public Sub RenameColumnTest()
        Dim task As New RenameColumnTask(C.Type1Name, C.Column1Name, "Ben", 1)
        task.Execute(metaData, context)
        Dim type As MetaDataType = metaData.FindType(C.Type1Name)
        Dim col As MetaDataColumn = type.Column(C.Column1Name)
        Assert.AreEqual("Ben", col.MemberName)
    End Sub

    <Test()> _
    Public Sub RenameAssociationTest()
        Dim task As New RenameAssociationTask(C.Type1Name, C.Association1Name, "Ben", 1)
        task.Execute(metaData, context)
        Dim type As MetaDataType = metaData.FindType(C.Type1Name)
        Dim assoc As MetaDataAssociation = type.Association(C.Association1Name)
        Assert.AreEqual("Ben", assoc.MemberName)
    End Sub


    <Test()> _
    Public Sub SortTasksTest()
        Dim list As New List(Of BaseMetaDataTask)
        Dim task1 As New RenameAssociationTask(C.Type1Name, C.Association1Name, "Ben", 10)
        list.Add(task1)
        Dim task2 As New RenameAssociationTask(C.Type1Name, C.Association1Name, "Ben", 2)
        list.Add(task2)
        Assert.AreEqual(task1, list(0), "The initial condition was wrong")
        list.Sort()
        Assert.AreEqual(task2, list(0), "The post condition was wrong")

    End Sub

    <Test()> _
    Public Sub BaseAggTest()
        Dim task As New BaseAggregateTask(Of Object)
        task.Execute(metaData, context)
    End Sub

    <Test()> _
    Public Sub AggAssociationTest()
        Dim task As New AggAssocTest
        Dim total As Integer = metaData.FindAllAssociations().Count
        task.Execute(metaData, context)
        Assert.AreEqual(total, task.Count)
    End Sub

    <Test()> _
    Public Sub AggColumnTest()
        Dim task As New AggColTest
        Dim total As Integer = metaData.FindAllColumns().Count
        task.Execute(metaData, context)
        Assert.AreEqual(total, task.Count)
    End Sub

    <Test()> _
    Public Sub AggTableTest()
        Dim task As New AggTableTest
        Dim total As Integer = metaData.FindAllTables().Count
        task.Execute(metaData, context)
        Assert.AreEqual(total, task.Count)
    End Sub

    <Test()> _
    Public Sub AggTypeTest()
        Dim task As New AggTypeTest
        Dim total As Integer = metaData.FindAllTypes().Count
        task.Execute(metaData, context)
        Assert.AreEqual(total, task.Count)
    End Sub

End Class
